home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- #include <GL/glx.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <X11/keysym.h>
-
- static int RGB_attributes[] =
- {
- GLX_RGBA,
- GLX_RED_SIZE, 1,
- GLX_GREEN_SIZE, 1,
- GLX_BLUE_SIZE, 1,
- None,
- };
-
- static float rgbMap[8][3] =
- {
- {1, 0, 0},
- {0, 1, 0},
- {1, 1, 0},
- {0, 0, 1},
- {1, 0, 1},
- {0, 1, 1},
- {1, 1, 1}
- };
-
- static long W = 650;
- static long H = 300;
-
- #define NUM_DESCENDENTS 7
-
- Display *dpy;
- int nDescendants = NUM_DESCENDENTS;
- Window descendant[NUM_DESCENDENTS];
- GC gc;
- GLXContext cx;
- XColor black;
- int colorBase = 0;
-
- static void
- FillWindow(int color)
- {
- glClearColor(rgbMap[color][0],
- rgbMap[color][1],
- rgbMap[color][2], 0);
- glClear(GL_COLOR_BUFFER_BIT);
- }
-
- static void WindowPrint(Window win, int x, int y, char *message)
- {
- XDrawString(dpy, win, gc, x, y, message, strlen(message));
- }
-
- static char *text[] = {
- "This program creates a nest of seven",
- "single-buffered RGB windows. A single",
- "OpenGL rendering context is bound to each",
- "window in turn and a clear is done to the",
- "window (each window cleared to a different",
- "color). Finally, this message is drawn",
- "using X. If you can't read this message,",
- "the program didn't work.",
- NULL
- };
-
- static void
- ClearWindows(void)
- {
- Window win;
- int i;
-
- for (i = nDescendants - 1; i >= 0; i--) {
- glXMakeCurrent(dpy, descendant[i], cx);
- FillWindow((i + colorBase) % 7);
- }
- glFinish();
- win = descendant[nDescendants - 1];
- for(i=0; text[i] != NULL; i++) {
- WindowPrint(win, 20, 20 + 20 * i, text[i]);
- }
- }
-
- static Bool
- WaitForMapNotify(Display * d, XEvent * e, char *arg)
- {
- if ((e->type == MapNotify) && (e->xmap.window == (Window) arg)) {
- return GL_TRUE;
- }
- return GL_FALSE;
- }
-
- int
- main(int argc, char **argv)
- {
- XVisualInfo *vi;
- Colormap cmap;
- Window window;
- XSetWindowAttributes swa;
- XGCValues gcvals;
- XEvent event;
- GLboolean needDisplay;
- int i;
-
- dpy = XOpenDisplay(0);
- if (!dpy) {
- fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
- return -1;
- }
- vi = glXChooseVisual(dpy, DefaultScreen(dpy), RGB_attributes);
- if (!vi) {
- fprintf(stderr, "No singlebuffered rgba visual on \"%s\"\n",
- getenv("DISPLAY"));
- return -1;
- }
- cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
- AllocNone);
- black.red = 0;
- black.green = 0;
- black.blue = 0;
- XAllocColor(dpy, cmap, &black);
-
- swa.border_pixel = 0;
- swa.background_pixel = black.pixel;
- swa.colormap = cmap;
- swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
- | KeyReleaseMask;
- window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
- W, H,
- 0, vi->depth, InputOutput, vi->visual,
- CWBackPixel | CWBorderPixel | CWColormap | CWEventMask,
- &swa);
- gcvals.foreground = black.pixel;
- gc = XCreateGC(dpy, window, GCForeground, &gcvals);
- descendant[0] = window;
- for (i = 1; i < nDescendants; i++) {
- descendant[i] = XCreateWindow(dpy, descendant[i - 1], 10, 10,
- W - 20 * i, H - 20 * i,
- 0, vi->depth, InputOutput, vi->visual,
- CWBackPixel | CWBorderPixel | CWColormap | CWEventMask,
- &swa);
- XMapWindow(dpy, descendant[i]);
- }
- XSetStandardProperties(dpy, window, "jackobox", "jackobox",
- None, argv, argc, NULL);
- XSetWMColormapWindows(dpy, window, &window, 1);
- XMapWindow(dpy, window);
- XIfEvent(dpy, &event, WaitForMapNotify, (char *) window);
-
- cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
- if (!glXMakeCurrent(dpy, window, cx)) {
- fprintf(stderr, "Can't make window current to context\n");
- return -1;
- }
- needDisplay = GL_TRUE;
- for (;;) {
- do {
- XNextEvent(dpy, &event);
- switch (event.type) {
- case Expose:
- needDisplay = GL_TRUE;
- break;
- case ConfigureNotify:
- needDisplay = GL_TRUE;
- break;
- case KeyPress:
- {
- char buf[100];
- int rv;
- KeySym ks;
-
- rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
- switch (ks) {
- case XK_space:
- colorBase++;
- needDisplay = GL_TRUE;
- break;
- case XK_Escape:
- exit(0);
- }
- }
- break;
- }
- } while (XPending(dpy) != 0);
-
- if (needDisplay) {
- needDisplay = GL_FALSE;
- ClearWindows();
- }
- }
- }
-